﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IcbcOpenDemo.Models;
using IcbcOpenDemo.Models.Tools;
using System.Net;
namespace IcbcOpenDemo
{
    public static class DemoPayPer
    {
        public static void PayPer(string payperCName, string payperAccno, string paypeeCName, string paypeeAccno)
        {
            RequestPayPer payper = new RequestPayPer
            {
                tran_date = SeqNo.TranDate,
                tran_time = SeqNo.TranTime,
                f_seq_no = SeqNo.FSeqNo,
                zip_flag = "0",
                zip = "",
                settle_mode = "0",
                instr_count = "2",
                total_amount = "2",
                package_name = "testPay",
                rd = new List<RequestPayPerRd> 
                {
                    new RequestPayPerRd
                    {
                        uni_busi_id=SeqNo.FSeqNo,
                        i_seq_no="1",
                        schedule_date="",
                        pay_type="1",
                        payer_account=payperAccno,
                        payer_cnname=payperCName,
                        payer_enname="",
                        payee_account=paypeeAccno,
                        payee_cnname=paypeeCName,
                        payee_enname="",
                        io_flag="1",
                        same_city_flag="",
                        payee_city="",
                        payee_bank_no="",
                        payee_bank_name="",
                        currency="CNY",
                        amount="1",
                        purpose_code="",
                        purpose="工资",
                        summary="工资",
                        summary_en="",
                        postscript="工资",
                        ref_no="",
                        related_ref_no="",
                        erp_serial_no="",
                        erp_check_no="",
                        busi_code="",
                        ori_voucher_type="",
                        ori_voucher_no="",
                        ori_voucher_name="",
                        manager_card_no="",
                        manager_card_name=""
                    },
                    new RequestPayPerRd
                    {
                        uni_busi_id=SeqNo.FSeqNo,
                        i_seq_no="2",
                        schedule_date="",
                        pay_type="1",
                        payer_account=payperAccno,
                        payer_cnname=payperCName,
                        payer_enname="",
                        payee_account=paypeeAccno,
                        payee_cnname=paypeeCName,
                        payee_enname="",
                        io_flag="1",
                        same_city_flag="",
                        payee_city="",
                        payee_bank_no="",
                        payee_bank_name="",
                        currency="CNY",
                        amount="1",
                        purpose_code="",
                        purpose="工资",
                        summary="工资",
                        summary_en="",
                        postscript="工资",
                        ref_no="",
                        related_ref_no="",
                        erp_serial_no="",
                        erp_check_no="",
                        busi_code="",
                        ori_voucher_type="",
                        ori_voucher_no="",
                        ori_voucher_name="",
                        manager_card_no="",
                        manager_card_name=""
                    }
                }
            };

            //Console.WriteLine(qacc.GetPath());

            // 签名
            //string signString = RSAClass.RSAFromPkcs8.sign(qacc.ToString(), PRIVATE_KEY, "UTF-8");

            // 将API交给通用请求类处理
            RequestCommon common = new RequestCommon(Params.APP_ID, Params.APIServiceUrl, Params.PRIVATE_KEY, payper);

            //common.sign = signString;

            //common.biz_content = qacc.ToString();
            //common.biz_content = qacc.ToJsonString();

            //Console.WriteLine(common.sign);

            //Console.WriteLine(common.ToString());
            //Console.WriteLine(common.ToString());

            //Console.WriteLine(common.ActionString());
            //Console.WriteLine(common.PostContent());

            string stmp = common.GetStrToSign();
            string postContent = common.PostContent();

            Console.WriteLine(string.Format("待签名数据：{0}", common.GetStrToSign()));

            Console.WriteLine(string.Format("待Post数据：{0}", common.PostContent()));

            //Thread.Sleep(1000);
            Console.WriteLine("ActionString:" + common.ActionString());

            //Console.WriteLine(string.Format("签名结果：{0}", common.sign));
            //Console.ReadKey();

            /*
            HttpWebRequest request = WebRequest.Create(common.ActionString()) as HttpWebRequest;


            request.ProtocolVersion = HttpVersion.Version10;
            request.ContentType = "application/x-www-form-urlencoded;charset=GBK";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.Method = "POST";
            request.Timeout = 8000;  //100000=1秒
            request.ReadWriteTimeout = 30000;
            //request.ServicePoint.Expect100Continue = false;  //必须

            Encoding encode = Encoding.GetEncoding("UTF-8");
            byte[] bytesToPost = encode.GetBytes(common.PostContent());

            using (System.IO.Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytesToPost, 0, bytesToPost.Length);
            }

            System.IO.Stream responseStream = request.GetResponse().GetResponseStream();

            string strResponse;
            using (System.IO.StreamReader responseReader = new System.IO.StreamReader(responseStream, encode))
            {
                strResponse = responseReader.ReadToEnd();
            }
            responseStream.Close();

            Console.WriteLine("返回结果：");
            Console.WriteLine(strResponse);
            */
        }
    }
}
